package com.qiang.controller;

import com.github.benmanes.caffeine.cache.Cache;
import com.qiang.enums.ExceptionCode;
import com.qiang.util.CacheUtil;
import com.qiang.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @ClassName LoginController
 * @Author zhuhongqiang
 * @Date 2019/4/1 13:36
 */


@RestController
@RequestMapping("login")
public class LoginController extends BaseController {


    /**
     * 生成随机的唯一验证码(用作用户唯一标识)
     * @return
     */
    @RequestMapping(value = "/genRandomCode")
    public ResultEntity GenRandomCode() {
        String agent = UUID.randomUUID().toString();
        return buildResponseEntity(agent);
    }



    /**
     * 退出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/loginOut")
    public ResultEntity<?> loginOut(HttpServletRequest request) {
        //清除缓存
        return buildResponseEntity(ExceptionCode.SUCCESS);
    }


    @Autowired
    CacheUtil cacheUtil;
    /**
     * 退出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/cacheTest")
    public ResultEntity<?> cacheTest(HttpServletRequest request) {
        Cache<String, String> cache = cacheUtil.cacheBuilder();
        // 将数据放入本地缓存中
        cache.put("username", "afei");
        cache.put("password", "123456");
        // 从本地缓存中取出数据
        System.out.println(cache.getIfPresent("username"));
        System.out.println(cache.getIfPresent("password"));
        System.out.println(cache.get("blog", key -> {
            // 本地缓存没有的话，从数据库或者Redis中获取
            return "get from redis..";
        }));
        return buildResponseEntity(ExceptionCode.SUCCESS);
    }
}
